package com.qkl.xiatang.app.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.qkl.xiatang.app.dto.BusinessDTO;
import com.qkl.xiatang.app.dto.pond.PondBackupDTO;
import com.qkl.xiatang.app.dto.pond.PondBackupWebDTO;
import com.qkl.xiatang.app.entity.pond.PondBatchEntity;
import com.qkl.xiatang.app.mapper.PondBatchMapper;
import com.qkl.xiatang.app.utils.AppSessionUtils;
import com.qkl.xiatang.app.utils.AppUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@Service
public class PondBatchService {

    @Autowired
    private PondBatchMapper pondBatchMapper;

    @Autowired
    private AppThreadPoolService appThreadPool;

    @Autowired
    private PondStatusService pondStatusService;


    /**
     * 清塘
     * @param webDTO
     * @return
     */
    public BusinessDTO<Object> pondClean(PondBackupWebDTO webDTO){
        List<String> pondIdList = webDTO.getPondIdList();
        pondIdList = AppUtils.stringsDistinct(pondIdList);
        log.info("====pondClean.pondIdList={}",pondIdList);
        if(ObjectUtils.isEmpty(pondIdList)){
            return BusinessDTO.failBusiness("未选择塘口");
        }
        String companyId = AppSessionUtils.getCompanyId();
        QueryWrapper queryWrapper = new QueryWrapper<PondBatchEntity>().eq("company_id",companyId).eq("is_clean",0).in("pond_id",pondIdList).orderByDesc("create_time");
        List<PondBatchEntity> pondBatchList = pondBatchMapper.selectList(queryWrapper);
        log.info("====pondClean.pondBatchList={}",pondBatchList);
        if(ObjectUtils.isEmpty(pondBatchList) || pondBatchList.size() < pondIdList.size()){
            return BusinessDTO.failBusiness("塘口未创建批次或已清塘");
        }
        List<PondBatchEntity> backupList = new ArrayList<>();
        Map<String,String> map = new HashMap<>();
        for (PondBatchEntity pondBatch : pondBatchList ){
            String batchId = pondBatch.getId();
            if(!map.containsKey(batchId)){
                backupList.add(pondBatch);
            }
        }
        if(pondIdList.size() != backupList.size()){
            return BusinessDTO.failBusiness("存在塘口未创建批次或已清塘");
        }
        //backupData(backupList);
        pondBatchMapper.pondClean(companyId,pondIdList);
        pondBatchMapper.pondGroupClean(companyId,pondIdList);
        pondStatusService.pondStatusIdle(pondIdList,AppSessionUtils.getUserName());

        appThreadPool.execute(()->{
            backupData(backupList);
        });

        return BusinessDTO.sucessBusiness(new HashMap<>(),"清塘成功");
    }


    public void backupData(List<PondBatchEntity> backupList ){
        for (PondBatchEntity entity : backupList){
            PondBackupDTO params = new PondBackupDTO();
            params.setBatchId(entity.getId());
            params.setBatchName(entity.getName());
            params.setCompanyId(entity.getCompanyId());
            params.setPondIdList(new ArrayList<>());
            params.getPondIdList().add(entity.getPondId());

            this.backupFeedAllocQty(params);
            log.info("备份 backupFeedAllocQty 完成");
            this.backupFeedAllocDetail(params);
            log.info("备份 backupFeedAllocDetail 完成");
            this.backupFeedAlloc(params);
            log.info("备份 backupFeedAllocDetail 完成");
            this.backupZooAllocDetail(params);
            log.info("备份 backupZooAllocDetail 完成");
            this.backupUploadFile(params);
            log.info("备份 backupUploadFile 完成");
            this.backupSaleBatchDetail(params);
            log.info("备份 backupSaleBatchDetail 完成");
            this.backupSaleBatch(params);
            log.info("备份 backupSaleBatch 完成");
            this.backupFeedAllocCheck(params);
            log.info("备份 backupFeedAllocCheck 完成");
        }
        log.info("====================全部备份完成");
    }



    /**
     * 备份 feed_alloc_qty 表
     * @param params
     */
    @Transactional
    public void backupFeedAllocQty(PondBackupDTO params){
        pondBatchMapper.backupFeedAllocQty(params);
        pondBatchMapper.deleteFeedAllocQty(params);
    }

    /**
     * 备份 eed_alloc_detail 表
     * @param params
     */
    @Transactional
    public void backupFeedAllocDetail(PondBackupDTO params){
        pondBatchMapper.backupFeedAllocDetail(params);
        pondBatchMapper.deleteFeedAllocDetail(params);
    }

    /**
     * 备份 feed_alloc 表
     * @param params
     */
    @Transactional
    public void backupFeedAlloc(PondBackupDTO params){
        pondBatchMapper.backupFeedAlloc(params);
        pondBatchMapper.deleteFeedAlloc(params);
    }

    /**
     * 备份 zoo_alloc_detail 表
     * @param params
     */
    @Transactional
    public void backupZooAllocDetail(PondBackupDTO params){
        pondBatchMapper.backupZooAllocDetail(params);
        pondBatchMapper.deleteZooAllocDetail(params);
    }


    /**
     * 备份 upload_file 表
     * @param params
     */
    @Transactional
    public void backupUploadFile(PondBackupDTO params){
        pondBatchMapper.backupUploadFile(params);
        pondBatchMapper.deleteUploadFile(params);
    }

    /**
     * 备份 sale_batch 表
     * @param params
     */
    @Transactional
    public void backupSaleBatch(PondBackupDTO params){
        pondBatchMapper.backupSaleBatch(params);
        pondBatchMapper.deleteSaleBatch(params);
    }

    /**
     * 备份 sale_batch_detail 表
     * @param params
     */
    void backupSaleBatchDetail(PondBackupDTO params){
        pondBatchMapper.backupSaleBatchDetail(params);
        pondBatchMapper.deleteSaleBatchDetail(params);
    }

    /**
     * 备份 feed_alloc_check 表
     * @param params
     */
    void backupFeedAllocCheck(PondBackupDTO params){
        pondBatchMapper.backupFeedAllocCheck(params);
        pondBatchMapper.deleteFeedAllocCheck(params);
    }

}
